﻿#include "mywaveplot.h"

#define TextColor QColor(255,255,255)//白色
#define LColor QColor(0,255,255)//青色
#define BlackColor QColor(0,0,0)
#define Plot_NoColor QColor(0,0,0,0)

#define Plot_DotColor QColor(236,110,0)
#define Plot_LineColor QColor(180,252,253)

#define TextWidth 2
#define LineWidth 1

MyWavePlot::MyWavePlot(QWidget *parent) : QCustomPlot(parent)
{
    init_plotSlot();
    create_graph();
}

//初始化图表
void MyWavePlot::init_plotSlot()
{
    //设置可拖拽、可放大缩小
    //this->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes | QCP::iSelectLegend | QCP::iSelectPlottables);
    //设置四边都有坐标
    this->axisRect()->setupFullAxesBox(true);

    //设置坐标颜色/坐标名称颜色
    this->yAxis->setLabelColor(TextColor);
    this->xAxis->setLabelColor(TextColor);

    this->xAxis->setTickLabelColor(TextColor);
    this->yAxis->setTickLabelColor(TextColor);

    this->xAxis->setBasePen(QPen(TextColor, TextWidth));
    this->yAxis->setBasePen(QPen(TextColor, TextWidth));

    this->xAxis->setTickPen(QPen(TextColor, TextWidth));
    this->yAxis->setTickPen(QPen(TextColor, TextWidth));

    this->xAxis->setSubTickPen(QPen(TextColor, TextWidth));
    this->yAxis->setSubTickPen(QPen(TextColor, TextWidth));

    this->yAxis2->setLabelColor(TextColor);
    this->xAxis2->setLabelColor(TextColor);

    this->xAxis2->setTickLabelColor(TextColor);
    this->yAxis2->setTickLabelColor(TextColor);

    this->xAxis2->setBasePen(QPen(TextColor, TextWidth));
    this->yAxis2->setBasePen(QPen(TextColor, TextWidth));

    this->xAxis2->setTickPen(QPen(TextColor, TextWidth));
    this->yAxis2->setTickPen(QPen(TextColor, TextWidth));

    this->xAxis2->setSubTickPen(QPen(TextColor, TextWidth));
    this->yAxis2->setSubTickPen(QPen(TextColor, TextWidth));

    //不显示X轴和Y轴小刻度
    //this->xAxis->setTickLabels(false);
    //this->xAxis->setTicks(false);
    //this->yAxis->setSubTicks(false);

    //不显示X轴上边和Y轴右边刻度
    this->xAxis2->setTickLabels(false);
    this->xAxis2->setTicks(false);

    this->yAxis2->setTickLabels(false);
    this->yAxis2->setTicks(false);

    //设置画布背景色
    QLinearGradient plotGradient;
    plotGradient.setStart(0, 0);
    plotGradient.setFinalStop(0, 350);
    plotGradient.setColorAt(0, QColor(80, 80, 80));
    plotGradient.setColorAt(1, QColor(50, 50, 50));
    this->setBackground(plotGradient);

    //设置坐标背景色
    QLinearGradient axisRectGradient;
    axisRectGradient.setStart(0, 0);
    axisRectGradient.setFinalStop(0, 350);
    axisRectGradient.setColorAt(0, QColor(80, 80, 80));
    axisRectGradient.setColorAt(1, QColor(30, 30, 30));
    this->axisRect()->setBackground(axisRectGradient);

    //设置图例提示位置及背景色
    // ui->pCustomPlot_V->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop | Qt::AlignRight);
    // ui->pCustomPlot_V->axisRect()->insetLayout()->setInsetAlignment(0, Qt::Alignment);
    // ui->Custom_V->legend->setBrush(QColor(255, 255, 255, 200));
    this->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop | Qt::AlignRight);
    this->legend->setVisible(true);
    this->legend->setBorderPen(QPen(QColor(Qt::transparent)));
    this->legend->setBrush(QColor(Qt::transparent));

    QFont legendFont = font();
    legendFont.setPointSize(10);
    this->legend->setFont(legendFont);
    this->legend->setTextColor(QColor(Qt::white));
    this->legend->setSelectedFont(legendFont);
    this->legend->setSelectableParts(QCPLegend::spItems);
    this->replot();

    //设置可拖拽、横坐标可放大缩小
    //this->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);

    //设置点击图例颜色变化
    //this->axisRect(0)->setRangeZoomFactor(2, 1);

    //设置横坐标
    //this->yAxis->grid()->deleteLater();
    //this->xAxis->grid()->deleteLater();

    // 设置静态曲线的横坐标范围及自适应横坐标
    this->xAxis->setRange(0, 100);
    this->yAxis->setRange(-100, 100);

    //设置X/Y坐标名称
    this->yAxis->setLabel(tr(""));
    this->xAxis->setLabel(tr(""));
    //点击图例操作
    //connect(this, SIGNAL(legendDoubleClick(QCPLegend*,QCPAbstractLegendItem*,QMouseEvent*)), this,SLOT(legendDoubleClick_Press(QCPLegend*,QCPAbstractLegendItem*)));
    this->replot();
}

//初始化波形曲线
void MyWavePlot::create_graph()
{
    QList<QString> list_U;
    list_U<<"A"<<"B"<<"C";

    for(int i=0;i<3;i++)
    {
        this->addGraph();
        this->graph(i)->setLineStyle(QCPGraph::lsLine);
        //this->graph(i)->setScatterStyle(QCPScatterStyle::ssDisc);
        this->graph(i)->setPen(select_color(i));
        this->graph(i)->setName(list_U.at(i));//曲线名称
        this->graph(i)->rescaleValueAxis(true);
        this->graph(i)->setVisible(true);
        this->replot();
    }
}

//图标曲线颜色
QPen  MyWavePlot::select_color(int flag)
{
    QPen pc;
    if (flag==0)
    {
        pc=QPen(Qt::yellow,LineWidth);
    }
    else if (flag==1)
    {
        pc=QPen(Qt::green,LineWidth);
    }
    else if (flag==2)
    {
        pc=QPen(Qt::red,LineWidth);
    }
    return pc;
}

void MyWavePlot::setData(float* waveDataA,float* waveDataB,float* waveDataC,int dataLen)
{
    QVector<double> xBarVector;
    QVector<double> yBarVectorA,yBarVectorB,yBarVectorC;

    double maxV=0;
    double minV=0;
    double span=0;

    double a,b,c;

    for(int i=0;i<dataLen;i++)
    {
        xBarVector<<i;

        a=*(waveDataA+i);
        b=*(waveDataB+i);
        c=*(waveDataC+i);

        yBarVectorA<<a;
        yBarVectorB<<b;
        yBarVectorC<<c;

        if(a>maxV) maxV=a;
        if(b>maxV) maxV=b;
        if(c>maxV) maxV=c;
        if(a<minV) minV=a;
        if(b<minV) minV=b;
        if(c<minV) minV=c;
    }

    span=(maxV-minV)*0.01;
    this->yAxis->setRange(minV-span, maxV+span);
    this->xAxis->setRange(0, dataLen,Qt::AlignLeft);

    this->graph(0)->setData(xBarVector,yBarVectorA);
    this->graph(1)->setData(xBarVector,yBarVectorB);
    this->graph(2)->setData(xBarVector,yBarVectorC);
    this->replot();

}
